global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"
do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do"

* All required Stata modules are available in the /_packages and /_auxiliary folders
cap adopath - "/home/site/etc/stata/ado.nber"
cap adopath - "/home/site/etc/stata/ado.nber/updates"
adopath ++ "$SSDIMed/scripts/_packages"
adopath ++ "$SSDIMed/scripts/_auxiliary"
grstyle init 

cap log close 
log using $SSDIMed/scripts/logs/04c_entry_bootstrap.log, text replace 
cap mkdir "$SSDIMed/results/estimates/modelparameterization"



****THIS FILE PREPARES MTO FIGURES WITH CIs for BASELINE and SIX VARIANTS REPORTED FOR ROBUSTNESS
***Specs for Figure fig:modelrobustness6 (A.9) that reports marginal medical spending for various entry and spending specs 
***Panel A= 54 & 55
***Panel B= Entry: County. Spending: Years enrolled X County, County X year of observation
***Panel C= Entry: Entry-Month. Spending: Years enrolled X County 
***Panel D= Entry: County. Spending: years enrolled X entry month 
***Panel E= Entry: null. Spending: Years enrolled X county 
***Panel F: Entry: county. Spending: Years enrolled. 

***Bootstrap for Entry needs to do 54&55, county, entry-month, and null. 
*************************************************************************************************


** STEP 0: CREATE BOOTSTRAP SAMPLES

set seed 20170526
cap mkdir "$SSDIMed/data/analysis/bootstrap"
local bsnum 500

forval n=1/`bsnum'{
use fipscounty_firstnm  covstart_month using "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear 
rename (fipscounty_firstnm ) (fipscounty_init)
gduplicates drop _all, force 
bsample 
rename * *`n'
gen clustervar`n'=_n
gen counter=_n 
save "$SSDIMed/data/analysis/bootstrap/sample`n'.dta", replace
}

use "$SSDIMed/data/analysis/bootstrap/sample1.dta", clear 
forval n=2/`bsnum'{
qui merge 1:1 counter using "$SSDIMed/data/analysis/bootstrap/sample`n'", nogen 
}
save "$SSDIMed/data/analysis/bootstrap/samples.dta", replace 



 
****STEP 1 ITERATE OVER SPECIFICATIONS and BOOTSTRAP SAMPLES, ESTIMATE TRUE SAMPLE COEFFICIENTS AS SAMPLE==0 ENTRY 

foreach spec in 02 5657 01 03{
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear 
if "`spec'"=="5657" keep if inrange(age_year_covstart_fill, 56, 57)
else keep if inrange(age_year_covstart_fill, 51, 52)
if "`spec'"=="5657" gen age52 = (age_year_covstart_fill == 57)
else gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
***define controls
local controls_01 ""
local controls_02 i.fipscounty_firstnm_g
local controls_03 i.covstart_month
local y incidence_pop_age_atapp
local w pop_age_atapp
local clust county_mofd
***run regression
if "`spec'"=="5657" local ctrl "02"
else local ctrl "`spec'"
if "`spec'"=="01" reg `y' age52 UR age52xUR [w=`w'],  cluster(`clust')
else reghdfe `y' age52 UR age52xUR [w=`w'], abs(`controls_`ctrl'')  cluster(`clust')
***store results 
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]

cd "$SSDIMed/results/estimates/x-age52xUR/"
cap postclose entry_unempatappx52
postfile entry_unempatappx52 intercept UR age52  age52xUR sample using "bootstrap_incidence_spec`spec'.dta", replace 
post entry_unempatappx52 (`intercept') (`UR') (`age52') (`age52xUR') (0)

***ITERATED OVER BOOTSTRAP SAMPLES
local bsnum 500
forval n=1/`bsnum'{
di "`n'"
qui{
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear  
if "`spec'"=="5657" keep if inrange(age_year_covstart_fill, 56, 57)
else keep if inrange(age_year_covstart_fill, 51, 52)
if "`spec'"=="5657" gen age52 = (age_year_covstart_fill == 57)
else gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR

local controls_01 ""
local controls_02 i.fipscounty_firstnm_g
local controls_03 i.covstart_month
local y incidence_pop_age_atapp
local w pop_age_atapp
local clust county_mofd
if "`spec'"=="5657" local ctrl "02"
else local ctrl "`spec'"
rename (fipscounty_firstnm covstart_month ) (fipscounty_init`n' covstart_month`n')
joinby fipscounty_init`n' covstart_month`n' using "$SSDIMed/data/analysis/bootstrap/samples.dta"
rename (fipscounty_init`n' covstart_month`n') (fipscounty_init covstart_month ) 
if "`spec'"=="01" reg `y' age52 UR age52xUR [w=`w'], 
else reghdfe `y' age52 UR age52xUR [w=`w'], abs(`controls_`ctrl'')  
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post entry_unempatappx52 (`intercept') (`UR') (`age52') (`age52xUR') (`n') 
}
}
postclose entry_unempatappx52
}

